package com.zhejiangzhengyuan.municipal_bi.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhejiangzhengyuan.municipal_common.entity.Device;
import com.zhejiangzhengyuan.municipal_common.entity.PumpStation;
import com.zhejiangzhengyuan.municipal_common.response.ResponseVo;
import com.zhejiangzhengyuan.municipal_common.service.BIService;
import com.zhejiangzhengyuan.municipal_common.service.DeviceService;
import com.zhejiangzhengyuan.municipal_common.service.PumpStationService;
import com.zhejiangzhengyuan.municipal_common.service.UserInfoService;
import com.zhejiangzhengyuan.municipal_common.utils.ConstantUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ZhouXiang
 * @since on 2020/1/13
 */
@RestController
@RequestMapping("/biData")
@Api(tags = "大数据展示Api")
public class BIController {

  @Autowired UserInfoService userInfoService;
  @Autowired PumpStationService pumpStationService;
  @Autowired DeviceService deviceService;
  @Autowired BIService biService;

  @ApiOperation("获取人员数量")
  @RequestMapping(value = "/getUserSize", method = RequestMethod.GET)
  public ResponseVo getUserSize() {
    return ResponseVo.successWithData(userInfoService.list().size());
  }

  @ApiOperation("获取巡检班次和整改率")
  @RequestMapping(value = "/getInspections", method = RequestMethod.GET)
  public ResponseVo getInspections(
      @ApiParam(value = "granularity", required = true, name = "粒度 年/月/季") String granularity) {
    // granularity粒度 年 月 季
    return ResponseVo.successWithData(biService.getInspectionsBIData(granularity));
  }

  @ApiOperation("获取桥梁和道路12个月的养护次数")
  @RequestMapping(value = "/getMaintenanceCount", method = RequestMethod.GET)
  public ResponseVo getMaintenanceConserve() {
    return ResponseVo.successWithData(biService.getMaintenanceConserve());
  }

  @ApiOperation("获取实时监控次数")
  @RequestMapping(value = "/getMonitorCount", method = RequestMethod.GET)
  public ResponseVo getMonitorCount() {
    return ResponseVo.successWithData(biService.getMonitorCount());
  }

  @ApiOperation("获取指定类型的告警")
  @RequestMapping(value = "/getSpecificWarnCount", method = RequestMethod.GET)
  public ResponseVo getSpecificWarnCount() {
    return ResponseVo.successWithData(biService.getSpecificWarnCount());
  }

  @ApiOperation("地图页面信息")
  @RequestMapping(value = "/getMapInformation", method = RequestMethod.GET)
  public ResponseVo getMapInformation() {
    return ResponseVo.successWithData(biService.getMapInformation());
  }

  @ApiOperation("道路巡检统计")
  @GetMapping(value = "/mobileTerminalRoadDisplay")
  public ResponseVo mobileTerminalRoadDisplay(String granularity) {
    String type = "道路";
    return ResponseVo.successWithData(biService.getBICount(type, null, granularity));
  }

  @ApiOperation("泵站专题展示")
  @GetMapping("/getPumpData")
  public ResponseVo getPumpData(String granularity) {
    return biService.getPumpData(ConstantUtil.WARN_TYPE.PUMPING_STATION, null, granularity);
  }

  @ApiOperation("获取12个月积水点信息")
  @GetMapping("/selectProblemsLineChart")
  public ResponseVo selectProblemsLineChart() {
    return biService.selectProblemsLineChart();
  }

  @ApiOperation("获取每个积水报警信息")
  @GetMapping("/selectDeviceWarnCharts")
  public ResponseVo selectDeviceWarnCharts(String granularity) {
    return biService.selectDeviceWarnCharts(granularity);
  }

  @ApiOperation("获取积水点数量")
  @GetMapping("/getWaterRulerCount")
  public ResponseVo getWaterRulerCount() {
    QueryWrapper<Device> deviceQueryWrapper = new QueryWrapper<>();
    deviceQueryWrapper.like("name", "水尺");
    JSONObject result = new JSONObject();
    result.put("count", deviceService.count(deviceQueryWrapper));
    return ResponseVo.successWithData(result);
  }

  @ApiOperation("获取排水泵站信息")
  @GetMapping("/getPumpCount")
  public ResponseVo getPumpCount() {
    int count;
    int all = 0;
    JSONObject result = new JSONObject();
    QueryWrapper<PumpStation> pumpStationQueryWrapper = new QueryWrapper<>();
    pumpStationQueryWrapper.eq("type", "雨水泵");
    count = pumpStationService.count(pumpStationQueryWrapper);
    all += count;
    result.put("雨水泵", count);
    pumpStationQueryWrapper = new QueryWrapper<>();
    pumpStationQueryWrapper.eq("type", "污水泵");
    count = pumpStationService.count(pumpStationQueryWrapper);
    all += count;
    result.put("污水泵", count);
    pumpStationQueryWrapper = new QueryWrapper<>();
    pumpStationQueryWrapper.eq("type", "雨污水泵");
    count = pumpStationService.count(pumpStationQueryWrapper);
    all += count;
    result.put("雨污水泵", count);
    result.put("总数", all);
    return ResponseVo.successWithData(result);
  }

  @ApiOperation("市政道路专题")
  @GetMapping(value = "/municipalRoad")
  public ResponseVo municipalRoad(String granularity) {
    String type = "道路";
    return biService.municipalBI(type, granularity);
  }

  @ApiOperation("市政桥梁专题")
  @GetMapping(value = "/municipalBridge")
  public ResponseVo municipalBridge(String granularity) {
    String type = "桥梁";
    return biService.municipalBI(type, granularity);
  }
}
